//
//  FindViewController.m
//  quickLook
//
//  Created by J on 15/8/30.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "FindViewController.h"
#import "RightSearchController.h"
#import "Header.h"

#import "Model.h"

#import "ADScrollView.h"

#import "FindScrollView.h"

#import "CustomView.h"

#import "FindViewSeeMoreController.h"


@interface FindViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSNumber* _number;
    BOOL _isLoad;
    
    
    
}
@property(nonatomic,assign)BOOL isLoad;

@property(nonatomic,strong)UITableView* tableView;

@property(nonatomic,strong)NSMutableArray* dataArray;

@property(nonatomic,strong)NSMutableArray* ScrolldataArray;

@property(nonatomic,strong)NSMutableArray* ScrollViewsArray;


@property(nonatomic,strong)ADScrollView* adScrollView;

@property(nonatomic,strong)FindScrollView * findScrollView;



@property(nonatomic,strong)NSMutableArray* titleArray;


@end

@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _number = @0;

    
    self.automaticallyAdjustsScrollViewInsets=NO;
    [self createUI];
    [self loadData];

    
}
- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];
    
    

}

- (void)loadData{
    
    
    
    //如果没网 直接停止动画
//    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
//    BOOL isConnection=NO;
//    
//    isConnection=[userdefault boolForKey:@"isConnection"];

    
    //获取广告页数据
    [DownLoad getFinderViewBannersData:^(id obj, NSError *error) {
        [_tableView.header endRefreshing];
        NSArray* comics=  obj[@"data"][@"banner_group"];
        _dataArray =[NSMutableArray array];
     NSMutableArray*   valueArray =[NSMutableArray array];
        [comics enumerateObjectsUsingBlock:^(NSDictionary* dic, NSUInteger idx, BOOL *stop) {
            NSString* pic=dic[@"pic"];
            Model* model=[[Model alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];

            [valueArray addObject:model];
          [_dataArray addObject:pic];
            
            
        }];
        
        
        
        
            //广告数据
          ADScrollView* adView=[[ADScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FindCellHEIGHT) andImageArr:_dataArray andValueArray:valueArray];
        
        adView.delegate=self;
         _tableView.tableHeaderView= adView;

        [_tableView reloadData];

    }];
    
    
    
    //获取cell数据
    
    
    
    
    
    NSString* url=@"topic_lists/mixed";
    
  
    
        _ScrollViewsArray=[NSMutableArray array];
    
        [DownLoad getFinderViewSrollsDataWithUrl:url andBlock:^(NSDictionary* obj, NSError * error) {
            
            

            
            [_tableView.header endRefreshing];

            
            NSArray* comics= obj[@"data"][@"topics"];

         
            _titleArray=[NSMutableArray array];

            
            
            [comics enumerateObjectsUsingBlock:^(NSDictionary* dic, NSUInteger idx, BOOL *stop) {
                

                
                Model* titleModelodel=[[Model alloc]init];
                
                [titleModelodel setValuesForKeysWithDictionary:dic];

                
              [_titleArray addObject:titleModelodel];
                
                //官方活动不展示
                if ([titleModelodel.title isEqualToString:@"官方活动"]) {
                    [_titleArray removeLastObject];
                }
                

                if(idx==0){
                    

                    NSMutableArray* dataArray=[NSMutableArray array];
                    NSMutableArray*  ModelArray =[NSMutableArray array];

                    NSArray *array= dic[@"topics"];

                    [array enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) {
                        
                        
                        NSString* vertical_image_url= obj[@"vertical_image_url"];
                        
                        Model* model=[[Model alloc]init];

                        [model setValuesForKeysWithDictionary:obj];
                        

                        
                        [ModelArray addObject:model];
                        [dataArray addObject:vertical_image_url];
                        
                    }];
                    
                   FindScrollView*  findScrollView=[[FindScrollView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH + (dataArray.count-1 )*5 , FindCellHEIGHT) andImageArr:dataArray andIdArray:ModelArray andTitle:dic[@"title"]];
                    
                    
                    findScrollView.delegate=self;
                    [_ScrollViewsArray addObject:findScrollView];
                    
                    
                    
                    
                    
                    
                }
                
                
                
                
                
                if (idx==1) {

                    NSMutableArray*  ModelArray =[NSMutableArray array];

                    NSMutableArray* dataArray=[NSMutableArray array];
                    
                    NSArray *array= dic[@"topics"];
                    

                    [array enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) {
                        
                        
                        NSString* vertical_image_url= obj[@"vertical_image_url"];
                
                        Model* model=[[Model alloc]init];

                        
                        [model setValuesForKeysWithDictionary:obj];
                        
                        
                        
                        [ModelArray addObject:model];

                        [dataArray addObject:vertical_image_url];
                        
                    }];
                    
                    FindScrollView*  findScrollView=[[FindScrollView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH + (dataArray.count-1 )*5 , FindCellHEIGHT) andImageArr:dataArray andIdArray:ModelArray andTitle:dic[@"title"]];
                    findScrollView.delegate=self;

                    [_ScrollViewsArray addObject:findScrollView];
                    
                }
                
                
                
                
                
                if (idx==2) {
                    JZJLog(@"2");

                    NSMutableArray*  ModelArray =[NSMutableArray array];

                    NSMutableArray* dataArray=[NSMutableArray array];
                    
                    NSArray *array= dic[@"topics"];
                    

                    [array enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) {
                        
                        
                        
                        JZJLog(@"%zd",array.count);
                        NSString* cover_image_url= obj[@"cover_image_url"];
                        
                        
                        Model* model=[[Model alloc]init];

                        
                        
                        
                        [model setValuesForKeysWithDictionary:obj];
                        
                        
                        
                        [ModelArray addObject:model];

                        
                        
                        [dataArray addObject:cover_image_url];
                        
                    }];
                    FindScrollView*  findScrollView=[[FindScrollView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH + (dataArray.count-1 )*5 , FindCellHEIGHT) andImageArr:dataArray andIdArray:ModelArray andTitle:dic[@"title"]];
                    findScrollView.delegate=self;

                    [_ScrollViewsArray addObject:findScrollView];
                    
                }
                
                
                
                
                
                
                
                if (idx==3) {
                    JZJLog(@"3");

                    
                    NSMutableArray*  ModelArray =[NSMutableArray array];

                    NSMutableArray* dataArray=[NSMutableArray array];
                    
                    
                    NSArray *array= dic[@"topics"];

                    
                    [array enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) {
                        
                        
                        NSString* cover_image_url= obj[@"vertical_image_url"];
                        
                        
                        [dataArray addObject:cover_image_url];
                        
                        Model* model=[[Model alloc]init];

                        
                        [model setValuesForKeysWithDictionary:obj];
                        
                        
                        
                        [ModelArray addObject:model];

                        
                        
                        
                    }];
                    
                    FindScrollView*  findScrollView=[[FindScrollView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH + (dataArray.count-1 )*5 , FindCellHEIGHT) andImageArr:dataArray andIdArray:ModelArray andTitle:dic[@"title"]];
                    
                    findScrollView.delegate=self;

                    [_ScrollViewsArray addObject:findScrollView];
                    
                    
                }
                

                
                if(idx==5){
                    
                    JZJLog(@"5");

                    NSMutableArray*  ModelArray =[NSMutableArray array];

                    
                    NSMutableArray* dataArray=[NSMutableArray array];
                    
                    
                    
                    NSArray *array= dic[@"topics"];
                    

                    
                    [array enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) {
                        
                        
                        NSString* vertical_image_url= obj[@"vertical_image_url"];
                        Model* model=[[Model alloc]init];

                        
                        [model setValuesForKeysWithDictionary:obj];
                        
                        
                        
                        [ModelArray addObject:model];

                        
                        [dataArray addObject:vertical_image_url];
                        
                    }];
                    
                    FindScrollView*  findScrollView=[[FindScrollView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH + (dataArray.count-1 )*5 , FindCellHEIGHT) andImageArr:dataArray andIdArray:ModelArray andTitle:dic[@"title"]];
                    
                    findScrollView.delegate=self;

                    [_ScrollViewsArray addObject:findScrollView];
                  
                }
         
                [_tableView reloadData];


            }];
          

        }];
    
    
    
    _isLoad=NO;

    
    
    
    
 
}




- (void)createUI
{
    
    UIImage* image=[[[UIImage imageNamed:@"magnifier.png"]TransformtoSize:CGSizeMake(20, 20)]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(push)];
    
    
    
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64,SCREEN_WIDTH  , SCREEN_HEIGHT-64-49) style:UITableViewStyleGrouped];
    
    _tableView.delegate=self;
    _tableView.dataSource=self;
    
    [self.view addSubview:_tableView];
    
    

  
    
    
    WS(weakSelf)
    
    //添加下拉刷新
    [_tableView addLegendHeaderWithRefreshingBlock:^{// 每次下拉都会产生效果  所以需要一次操作来停止效果
        if (!weakSelf.isLoad) {
            //开始数据请求
            _number=@0;
            weakSelf.isLoad=!weakSelf.isLoad;
            [weakSelf loadData];
        }
        else
        {
            [weakSelf.tableView.header endRefreshing];//如果正在运行 的时候拉一下会进入else但是 会产生效果   需要停止这个效果 每次下拉都会产生效果  所以需要一次操作来停止效果
            
        }
    }];
    
    
    
    
    
    
}



//跳转到查看更多页面

- (void)seeMoreWithModel:(NSInteger)tag

{
    
    
    FindViewSeeMoreController* vc=[[FindViewSeeMoreController alloc]init];
    
    Model* model=_titleArray[tag];
    vc.model=model;
    
    vc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc animated:YES];
  
    
    


}






#pragma mark --UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    JZJLog(@"1%zd",_dataArray.count);

    return _ScrollViewsArray.count;
    

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"ID"];
    
    if (!cell) {
        
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
        
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
      
        

    }
    FindScrollView* vc=nil;
    
    JZJLog(@"2%zd",_dataArray.count);

    
    if (_ScrollViewsArray.count) {
        
       vc=_ScrollViewsArray[indexPath.section];

    }
        
   

    
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [cell.contentView addSubview: vc];

    
    return cell;
 
    
    
}


#pragma mark --UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return FindCellHEIGHT;
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{


    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{

    
    return 1;

}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    
    CustomView* view= (CustomView*)[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"FindTableHeaderVIew"];
    
    if (!view) {
       view=[[NSBundle mainBundle]loadNibNamed:@"FindTableHeaderVIew" owner:nil options:nil][0];

        view.findVCDelegate=self;
    }

        Model*model=_titleArray[section];
    
        [view refreshFindViewControllerTableHeaderView:model];

    


    
    return view;



}




- (void)push {
    
    
    
    RightSearchController* vc=[[RightSearchController alloc]init];
    
    vc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}

@end
